﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_ManageDB : System.Web.UI.Page
{
    protected string strModifySelected
    {
        get
        {
            return Session["xDocModifySelected"].ToString();
        }
        set
        {
            Session["xDocModifySelected"] = value;
        }
        //get
        //{
        //    return (XDocument)Cache["xDocQuizAssignment" + User.Identity.Name.ToString()];
        //}
        //set
        //{
        //    Cache["xDocQuizAssignment" + User.Identity.Name.ToString()] = (XDocument)value;
        //}
    }

    //XDocument xDocModifySelected = new XDocument();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

        }
    }

    protected void btnOpenAddToDB_Click(object sender, EventArgs e)
    {
        MultiViewDB.ActiveViewIndex = 0;
    }
    protected void btnOpenModifyDB_Click(object sender, EventArgs e)
    {
        MultiViewDB.ActiveViewIndex = 1;
        Fill_ddlistModifyClasses();
        Fill_ddlistModifySections(0);
        Fill_ddlistModifyAssignments();
        Initialize_xDocModifySelected();
    }

    //protected void btnOpenDeleteFromDB_Click(object sender, EventArgs e)
    //{
    //    MultiViewDB.ActiveViewIndex = 2;
    //}

    #region Add To Database

    protected void rdobtnsAddAccountType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rdobtnsAddAccountType.SelectedIndex == 0)
        {
            txtAddPassword.ValidationGroup = "AddUser";
            txtAddSecurityQuestion.ValidationGroup = "AddUser";
            txtAddSecurityAnswer.ValidationGroup = "AddUser";
            PanelAddNonInstitute.Visible = false;
        }
        else
        {
            txtAddPassword.ValidationGroup = "";
            txtAddSecurityQuestion.ValidationGroup = "";
            txtAddSecurityAnswer.ValidationGroup = "";
            PanelAddNonInstitute.Visible = true;
        }
    }

    protected void btnAddUser_Click(object sender, EventArgs e)
    {
        UsersBLL usersLogic = new UsersBLL();

        string NetworkID = txtAddNetworkID.Text;
        string IDNumber = txtAddIdNumber.Text;
        string FirstName = txtAddFirstName.Text;
        string LastName = txtAddLastName.Text;
        string Email = txtAddEmail.Text;
        int Year = Convert.ToInt32(ddlistAddYear.SelectedValue);

        bool result = false;

        // Institute Student
        if (rdobtnsAddAccountType.SelectedIndex == 0)
        {
            result = usersLogic.InsertUser(NetworkID, "Student", FirstName, LastName, IDNumber, Email, Year) != 0;
        }
        else // Non-Institue User
        {
            string hasedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtAddPassword.Text, "sha1");
            string SecurityQuestion = txtAddSecurityQuestion.Text;
            string SecurityAnswer = txtAddSecurityAnswer.Text;

            result = usersLogic.InsertNonInstitutUser(Email, hasedPassword,
                    "NonInstitut", FirstName, LastName, "", Year,
                    SecurityQuestion, SecurityAnswer) != 0;
        }

        if (result)
        {

        }
    }

    #endregion

    #region Modify Database

    #region Modify Assignment Submissions
    private void Initialize_xDocModifySelected()
    {
        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected.Declaration = new XDeclaration("1.0", "utf-8", "yes");

        xDocModifySelected.Document.Add(new XElement("dbmodify"));

        strModifySelected = xDocModifySelected.ToString();
    }

    protected void rdobtnsModifyAssignmentType_SelectedIndexChanged(object sender, EventArgs e)
    {
        Fill_ddlistModifyAssignments();
    }

    protected void Fill_ddlistModifyClasses()
    {
        ClassBLL classLogic = new ClassBLL();
        Classes.CoursesDataTable courses = classLogic.GetCourses();

        ddlistModifyClasses.Items.Clear();

        ListItem firstItem = new ListItem("--- Select Class ---", "0");
        ddlistModifyClasses.Items.Add(firstItem);

        foreach (Classes.CoursesRow course in courses)
        {
            ListItem newItem = new ListItem(course.Title, course.CourseID.ToString());
            ddlistModifyClasses.Items.Add(newItem);
        }
        ddlistModifyClasses.SelectedIndex = 0;
    }

    protected void ddlistModifyClasses_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlistModifyClasses.SelectedIndex != 0)
        {
            int courseID = Convert.ToInt32(ddlistModifyClasses.SelectedValue);
            Fill_ddlistModifySections(courseID);
            Fill_ddlistModifyAssignments();
        }
        else
        {
            Fill_ddlistModifySections(0);
            Fill_ddlistModifyAssignments();
        }

        ddlistModifySections.SelectedIndex = 0;
    }

    protected void Fill_ddlistModifySections(int CourseID)
    {
        ClassBLL classLogic = new ClassBLL();
        Classes.Courses_SectionDataTable sections = classLogic.GetSectionsByCourseID(CourseID, true);

        ddlistModifySections.Items.Clear();

        if (CourseID == 0)
        {
            ListItem firstItem = new ListItem("--- Select Class First ---", "0");
            ddlistModifySections.Items.Add(firstItem);
            ddlistModifySections.Enabled = false;
        }
        else
        {
            ListItem firstItem = new ListItem("--- Select Section if you'd like to ---", "0");
            ddlistModifySections.Items.Add(firstItem);

            foreach (Classes.Courses_SectionRow section in sections)
            {
                ListItem newItem = new ListItem(section.SectionNumber.ToString());
                ddlistModifySections.Items.Add(newItem);
            }

            ddlistModifySections.Enabled = true;
        }
    }

    protected void ddlistModifySections_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlistModifySections.SelectedIndex != 0)
        {
            Fill_ddlistModifyAssignments();
        }
        else
        {

        }
    }

    protected void rdobtnsModifyStandards_SelectedIndexChanged(object sender, EventArgs e)
    {
        Fill_ddlistModifyAssignments();
    }

    protected void Fill_ddlistModifyAssignments()
    {
        AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
        Assignments.AssignmentsDataTable quizAssignments = new Assignments.AssignmentsDataTable();

        string AssignmentType = rdobtnsModifyAssignmentType.SelectedValue;
        int CourseID = Convert.ToInt32(ddlistModifyClasses.SelectedValue);
        int SectionNumber = 0;
        if (ddlistModifySections.SelectedIndex != -1)
        {
            SectionNumber = Convert.ToInt32(ddlistModifySections.SelectedValue);
        }
        int StandardID = Convert.ToInt32(rdobtnsModifyStandards.SelectedValue);



        if (CourseID != 0 && SectionNumber != 0 && StandardID != 0)
        {
            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByCourseIdAndSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
            foreach (Assignments.RelationsRow relation in relations)
            {
                Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(relation.AssignmentID);

                if (assignment.AssignmentType == AssignmentType)
                    quizAssignments.ImportRow(assignment);
            }
        }
        else if (CourseID != 0 && SectionNumber != 0 && StandardID == 0)
        {
            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByCourseIdAndSectionNumber(CourseID, SectionNumber);
            foreach (Assignments.RelationsRow relation in relations)
            {
                Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(relation.AssignmentID);

                if (assignment.AssignmentType == AssignmentType)
                    quizAssignments.ImportRow(assignment);
            }
        }
        else if (CourseID != 0 && SectionNumber == 0 && StandardID == 0)
        {
            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByCourseId(CourseID);
            foreach (Assignments.RelationsRow relation in relations)
            {
                Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(relation.AssignmentID);

                if (assignment.AssignmentType == AssignmentType)
                {
                    if (quizAssignments.FindByAssignmentID(assignment.AssignmentID) == null)
                        quizAssignments.ImportRow(assignment);
                }
            }
        }
        else if (CourseID != 0 && SectionNumber == 0 && StandardID != 0)
        {
            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByCourseIdAndStandardId(CourseID, StandardID);
            foreach (Assignments.RelationsRow relation in relations)
            {
                Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(relation.AssignmentID);

                if (assignment.AssignmentType == AssignmentType)
                {
                    if (quizAssignments.FindByAssignmentID(assignment.AssignmentID) == null)
                        quizAssignments.ImportRow(assignment);
                }
            }
        }
        else
        {
            quizAssignments = assignmentsLogic.GetAssignmentsByType(AssignmentType);
        }

        ddlistModifyAssignments.Items.Clear();
        if (quizAssignments.Count == 0)
        {
            ListItem newItem = new ListItem("--- No Assignment ---", "0");
            ddlistModifyAssignments.Items.Add(newItem);
            ddlistModifyAssignments.Enabled = false;
        }
        else
        {
            ListItem newItem = new ListItem("--- Select Assignment ---", "0");
            ddlistModifyAssignments.Items.Add(newItem);
            foreach (Assignments.AssignmentsRow quizAssignment in quizAssignments)
            {
                ListItem item = new ListItem(quizAssignment.AssignmentTitle, quizAssignment.AssignmentID.ToString());
                ddlistModifyAssignments.Items.Add(item);
            }
            ddlistModifyAssignments.Enabled = true;
        }
    }

    protected void btnModifyFindSubmissions_Click(object sender, EventArgs e)
    {
        int CourseID = Convert.ToInt32(ddlistModifyClasses.SelectedValue);

        int AssignmentID = 0;
        try
        {
            AssignmentID = Convert.ToInt32(ddlistModifyAssignments.SelectedValue);
        }
        catch
        {
            AssignmentID = 0;
        }

        if (rdobtnsModifyAssignmentType.SelectedValue == "Quiz")
            Fill_RepeaterQuizSubmissions();
        else
            Fill_RepeaterEssaySubmissions();

        Initialize_xDocModifySelected();
    }

    AssignmentsSubmissionsBLL submissionsLogic = new AssignmentsSubmissionsBLL();

    private void Fill_RepeaterQuizSubmissions()
    {
        int CourseID = Convert.ToInt32(ddlistModifyClasses.SelectedValue);
        int SectionNumber = Convert.ToInt32(ddlistModifySections.SelectedValue);
        int AssignmentID = Convert.ToInt32(ddlistModifyAssignments.SelectedValue);
        string NetworkID = txtModifyUsername.Text;
        string LastName = txtModifyLastName.Text;

        Assignments.Assignments_Submission_QuizDataTable quizSubmissions = submissionsLogic.GetAllQuizSubmissions();

        Assignments.Assignments_Submission_QuizDataTable filteredQuizSubmissions = new Assignments.Assignments_Submission_QuizDataTable();


        if (AssignmentID != 0 && NetworkID != String.Empty && LastName != String.Empty)
        {
            foreach (Assignments.Assignments_Submission_QuizRow quizSubmission in quizSubmissions)
            {
                if (quizSubmission.AssignmentID == AssignmentID &&
                    quizSubmission.Username == NetworkID &&
                    quizSubmission.LastName == LastName)
                {
                    filteredQuizSubmissions.ImportRow(quizSubmission);
                }
            }
        }
        else if (AssignmentID != 0 && NetworkID != String.Empty && LastName == String.Empty)
        {
            foreach (Assignments.Assignments_Submission_QuizRow quizSubmission in quizSubmissions)
            {
                if (quizSubmission.AssignmentID == AssignmentID &&
                    quizSubmission.Username == NetworkID)
                {
                    filteredQuizSubmissions.ImportRow(quizSubmission);
                }
            }
        }
        else if (AssignmentID != 0 && NetworkID == String.Empty && LastName == String.Empty)
        {
            foreach (Assignments.Assignments_Submission_QuizRow quizSubmission in quizSubmissions)
            {
                if (quizSubmission.AssignmentID == AssignmentID)
                {
                    filteredQuizSubmissions.ImportRow(quizSubmission);
                }
            }
        }
        else if (AssignmentID == 0 && NetworkID != String.Empty && LastName == String.Empty)
        {
            foreach (Assignments.Assignments_Submission_QuizRow quizSubmission in quizSubmissions)
            {
                if (quizSubmission.Username == NetworkID)
                {
                    filteredQuizSubmissions.ImportRow(quizSubmission);
                }
            }
        }
        else if (AssignmentID == 0 && NetworkID == String.Empty && LastName != String.Empty)
        {
            foreach (Assignments.Assignments_Submission_QuizRow quizSubmission in quizSubmissions)
            {
                if (quizSubmission.LastName == LastName)
                {
                    filteredQuizSubmissions.ImportRow(quizSubmission);
                }
            }
        }
        else if (AssignmentID == 0 && NetworkID != String.Empty && LastName != String.Empty)
        {
            foreach (Assignments.Assignments_Submission_QuizRow quizSubmission in quizSubmissions)
            {
                if (quizSubmission.LastName == LastName && quizSubmission.Username == NetworkID)
                {
                    filteredQuizSubmissions.ImportRow(quizSubmission);
                }
            }
        }
        else
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "Please select an assignment or enter Network ID ", 2);
            return;
        }

        saved = filteredQuizSubmissions;
        RepeaterQuizSubmissions.DataSource = filteredQuizSubmissions.Rows;
        RepeaterQuizSubmissions.DataBind();
        RepeaterQuizSubmissions.Visible = true;
        RepeaterEssaySubmissions.Visible = false;
    }

    //*PROBLEM
    Assignments.Assignments_Submission_QuizDataTable saved = new Assignments.Assignments_Submission_QuizDataTable();

    protected void lnkbtnQuizHeaderSort_Command(object sender, CommandEventArgs e)
    {
        LinkButton Sender = ((LinkButton)sender);
        string SortBy = e.CommandName.ToString();
        string Sort = e.CommandArgument.ToString();

        Assignments.Assignments_Submission_QuizDataTable sortedData = saved;
        List<Assignments.Assignments_Submission_QuizRow> sortedList = null;

        switch (SortBy)
        {
            case "SortByNetworkID":
                if (Sort == "Ascend")
                {
                    sortedList = sortedData.OrderBy(p => p.Username).ToList();
                    Sender.CommandArgument = "Descend";
                }
                else
                {
                    sortedList = sortedData.OrderByDescending(p => p.Username).ToList();
                    Sender.CommandArgument = "Ascend";
                }
                break;
            case "SortByLastName":
                if (Sort == "Ascend")
                {
                    sortedList = sortedData.OrderBy(p => p.LastName).ToList();
                    Sender.CommandArgument = "Descend";
                }
                else
                {
                    sortedList = sortedData.OrderByDescending(p => p.LastName).ToList();
                    Sender.CommandArgument = "Ascend";
                }
                break;
            case "SortByFirstName":
                if (Sort == "Ascend")
                    sortedList = sortedData.OrderBy(p => p.FirstName).ToList();
                else
                    sortedList = sortedData.OrderByDescending(p => p.FirstName).ToList(); ;
                break;
            case "SortByAttempts":
                if (Sort == "Ascend")
                    sortedList = sortedData.OrderBy(p => p.Attempts).ToList();
                else
                    sortedList = sortedData.OrderByDescending(p => p.Attempts).ToList(); ;
                break;
            case "SortByGrade":
                if (Sort == "Ascend")
                {
                    sortedList = sortedData.OrderBy(p => p.GradePercentage).ToList();
                    Sender.CommandArgument = "Descend";
                }
                else
                {
                    sortedList = sortedData.OrderByDescending(p => p.GradePercentage).ToList();
                    Sender.CommandArgument = "Ascend";
                }
                break;
            case "SortBySubmittedTime":
                if (Sort == "Ascend")
                    sortedList = sortedData.OrderBy(p => p.SubmittedDateTime).ToList();
                else
                    sortedList = sortedData.OrderByDescending(p => p.SubmittedDateTime).ToList(); ;
                break;
        }

        RepeaterQuizSubmissions.DataSource = sortedList;
        RepeaterQuizSubmissions.DataBind();

    }

    protected void RepeaterQuizSubmissions_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Assignments.Assignments_Submission_QuizRow row = e.Item.DataItem as Assignments.Assignments_Submission_QuizRow;

            CheckBox chkboxQuizSubmission = e.Item.FindControl("chkboxQuizSubmission") as CheckBox;
            Label lblNetworkID = e.Item.FindControl("lblNetworkID") as Label;
            Label lblLastName = e.Item.FindControl("lblLastName") as Label;
            Label lblFirstName = e.Item.FindControl("lblFirstName") as Label;
            Label lblAttempts = e.Item.FindControl("lblAttempts") as Label;
            Label lblGrade = e.Item.FindControl("lblGrade") as Label;
            Label lblSubmittedTime = e.Item.FindControl("lblSubmittedTime") as Label;

            chkboxQuizSubmission.Text = row.QuizSubmissionID.ToString();
            lblNetworkID.Text = row.Username;
            lblLastName.Text = row.LastName;
            lblFirstName.Text = row.FirstName;
            lblAttempts.Text = row.Attempts.ToString();
            lblGrade.Text = row.GradePercentage.ToString("##0.##") + "%";
            lblSubmittedTime.Text = row.SubmittedDateTime.ToString();
        }
    }


    private void Fill_RepeaterEssaySubmissions()
    {
        int AssignmentID = Convert.ToInt32(ddlistModifyAssignments.SelectedValue);
        string NetworkID = txtModifyUsername.Text;
        string LastName = txtModifyLastName.Text;

        Assignments.Assignments_Submission_EssayDataTable essaySubmissions = submissionsLogic.GetAllEssaySubmissions();

        Assignments.Assignments_Submission_EssayDataTable filteredEssaySubmissions = new Assignments.Assignments_Submission_EssayDataTable();

        if (AssignmentID != 0 && NetworkID != String.Empty && LastName != String.Empty)
        {
            foreach (Assignments.Assignments_Submission_EssayRow essaySubmission in essaySubmissions)
            {
                if (essaySubmission.AssignmentID == AssignmentID &&
                    essaySubmission.Username == NetworkID &&
                    essaySubmission.LastName == LastName)
                {
                    filteredEssaySubmissions.ImportRow(essaySubmission);
                }
            }
        }
        else if (AssignmentID != 0 && NetworkID != String.Empty && LastName == String.Empty)
        {
            foreach (Assignments.Assignments_Submission_EssayRow essaySubmission in essaySubmissions)
            {
                if (essaySubmission.AssignmentID == AssignmentID &&
                    essaySubmission.Username == NetworkID)
                {
                    filteredEssaySubmissions.ImportRow(essaySubmission);
                }
            }
        }
        else if (AssignmentID != 0 && NetworkID == String.Empty && LastName == String.Empty)
        {
            foreach (Assignments.Assignments_Submission_EssayRow essaySubmission in essaySubmissions)
            {
                if (essaySubmission.AssignmentID == AssignmentID)
                {
                    filteredEssaySubmissions.ImportRow(essaySubmission);
                }
            }
        }
        else if (AssignmentID == 0 && NetworkID != String.Empty && LastName == String.Empty)
        {
            foreach (Assignments.Assignments_Submission_EssayRow essaySubmission in essaySubmissions)
            {
                if (essaySubmission.Username == NetworkID)
                {
                    filteredEssaySubmissions.ImportRow(essaySubmission);
                }
            }
        }
        else if (AssignmentID == 0 && NetworkID == String.Empty && LastName != String.Empty)
        {
            foreach (Assignments.Assignments_Submission_EssayRow essaySubmission in essaySubmissions)
            {
                if (essaySubmission.LastName == LastName)
                {
                    filteredEssaySubmissions.ImportRow(essaySubmission);
                }
            }
        }
        else if (AssignmentID == 0 && NetworkID != String.Empty && LastName != String.Empty)
        {
            foreach (Assignments.Assignments_Submission_EssayRow essaySubmission in essaySubmissions)
            {
                if (essaySubmission.LastName == LastName && essaySubmission.Username == NetworkID)
                {
                    filteredEssaySubmissions.ImportRow(essaySubmission);
                }
            }
        }
        else
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "Please select an assignment or enter Network ID ", 2);
            return;
        }

        RepeaterEssaySubmissions.DataSource = filteredEssaySubmissions.Rows;
        RepeaterEssaySubmissions.DataBind();
        RepeaterQuizSubmissions.Visible = false;
        RepeaterEssaySubmissions.Visible = true;
    }

    protected void RepeaterEssaySubmissions_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Assignments.Assignments_Submission_EssayRow row = e.Item.DataItem as Assignments.Assignments_Submission_EssayRow;

            CheckBox chkboxEssaySubmission = e.Item.FindControl("chkboxEssaySubmission") as CheckBox;
            Label lblNetworkID = e.Item.FindControl("lblNetworkID") as Label;
            Label lblLastName = e.Item.FindControl("lblLastName") as Label;
            Label lblFirstName = e.Item.FindControl("lblFirstName") as Label;
            CheckBox chkboxIsFinal = e.Item.FindControl("chkboxIsFinal") as CheckBox;
            Label lblGrade = e.Item.FindControl("lblGrade") as Label;
            Label lblSubmittedTime = e.Item.FindControl("lblSubmittedTime") as Label;

            chkboxEssaySubmission.Text = row.EssaySubmissionID.ToString();
            lblNetworkID.Text = row.Username;
            lblLastName.Text = row.LastName;
            lblFirstName.Text = row.FirstName;
            chkboxIsFinal.Checked = row.IsFinal;
            lblGrade.Text = row.Grade.ToString();
            lblSubmittedTime.Text = row.SubmittedDateTime.ToString();
        }
    }


    protected void chkboxQuizSubmission_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox obj = ((CheckBox)sender);
        int QuizSubmissionID = Convert.ToInt32(((CheckBox)sender).Text);

        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected = XDocument.Parse(strModifySelected);

        List<XElement> _selected = xDocModifySelected.Descendants("selected").Where(
                    el => Convert.ToInt32(el.Value) == QuizSubmissionID).ToList();

        if (((CheckBox)sender).Checked)
        {
            if (_selected.Count == 0)
            {
                xDocModifySelected.Element("dbmodify").Add(new XElement("selected", obj.Text));
            }
        }
        else
        {
            if (_selected.Count != 0)
            {
                xDocModifySelected.Descendants("selected").Where(el => Convert.ToInt32(el.Value) == QuizSubmissionID).Remove();
            }
        }

        strModifySelected = xDocModifySelected.ToString();
    }

    protected void chkboxEssaySubmission_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox obj = ((CheckBox)sender);
        int EssaySubmissionID = Convert.ToInt32(((CheckBox)sender).Text);

        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected = XDocument.Parse(strModifySelected);

        List<XElement> _selected = xDocModifySelected.Descendants("selected").Where(
                    el => Convert.ToInt32(el.Value) == EssaySubmissionID).ToList();

        if (((CheckBox)sender).Checked)
        {
            if (_selected.Count == 0)
            {
                xDocModifySelected.Element("dbmodify").Add(new XElement("selected", obj.Text));
            }
        }
        else
        {
            if (_selected.Count != 0)
            {
                xDocModifySelected.Descendants("selected").Where(el => Convert.ToInt32(el.Value) == EssaySubmissionID).Remove();
            }
        }

        strModifySelected = xDocModifySelected.ToString();
    }

    protected void btnQuizDelete_Click(object sender, EventArgs e)
    {
        bool result = true;
        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected = XDocument.Parse(strModifySelected);

        List<XElement> _selected = xDocModifySelected.Descendants("selected").ToList();
        foreach (XElement selected in _selected)
        {
            if (result == true)
            {
                result = submissionsLogic.DeleteQuizSubmission(Convert.ToInt32(selected.Value));
            }
            else
            {
                ModalPopupMessage.ShowModalPopup("ILE Message", "Failed to Delete the selected quiz submissions.", 2);
                return;
            }
        }

        ModalPopupMessage.ShowModalPopup("ILE Message", "Selected quiz submissions have been successfully deleted.", 1);
        Fill_RepeaterQuizSubmissions();
    }

    protected void btnEssayDelete_Click(object sender, EventArgs e)
    {
        bool result = true;
        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected = XDocument.Parse(strModifySelected);
        List<XElement> _selected = xDocModifySelected.Descendants("selected").ToList();
        foreach (XElement selected in _selected)
        {
            if (result == true)
            {
                result = submissionsLogic.DeleteEssaySubmission(Convert.ToInt32(selected.Value));
            }
            else
            {
                ModalPopupMessage.ShowModalPopup("ILE Message", "Failed to Delete the selected essay submissions.", 2);
                return;
            }
        }

        ModalPopupMessage.ShowModalPopup("ILE Message", "Selected essay submissions have been successfully deleted.", 1);
        Fill_RepeaterEssaySubmissions();
    }

    protected void btnModifyQuizOpenMoveTo_Click(object sender, EventArgs e)
    {
        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected = XDocument.Parse(strModifySelected);

        List<XElement> _selected = xDocModifySelected.Descendants("selected").ToList();
        if (_selected.Count == 0)
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "At least one submission must be selected.", 2);
        }
        else
        {
            Fill_ddlistModifyQuizMoveToClass();
            Fill_ddlistModifyQuizMoveToSection(0);
            ModalPopupExtenderModifyQuizMoveTo.Show();
        }
    }

    protected void btnModifyEssayOpenMoveTo_Click(object sender, EventArgs e)
    {
        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected = XDocument.Parse(strModifySelected);

        List<XElement> _selected = xDocModifySelected.Descendants("selected").ToList();
        if (_selected.Count == 0)
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "At least one submission must be selected.", 2);
        }
        else
        {
            Fill_ddlistModifyEssayMoveToClass();
            Fill_ddlistModifyEssayMoveToSection(0);
            ModalPopupExtenderModifyEssayMoveTo.Show();
        }
    }

    protected void Fill_ddlistModifyQuizMoveToClass()
    {
        ClassBLL classLogic = new ClassBLL();
        Classes.CoursesDataTable courses = classLogic.GetCourses();

        ddlistModifyQuizMoveToClass.Items.Clear();

        ListItem firstItem = new ListItem("--- Select Class ---", "0");
        ddlistModifyQuizMoveToClass.Items.Add(firstItem);

        foreach (Classes.CoursesRow course in courses)
        {
            ListItem newItem = new ListItem(course.Title, course.CourseID.ToString());
            ddlistModifyQuizMoveToClass.Items.Add(newItem);
        }
        ddlistModifyQuizMoveToClass.SelectedIndex = 0;
    }

    protected void ddlistModifyQuizMoveToClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlistModifyQuizMoveToClass.SelectedIndex != 0)
        {
            int courseID = Convert.ToInt32(ddlistModifyQuizMoveToClass.SelectedValue);
            Fill_ddlistModifyQuizMoveToSection(courseID);
        }
        else
        {
            Fill_ddlistModifyQuizMoveToSection(0);
        }

        ddlistModifyQuizMoveToSection.SelectedIndex = 0;

        ModalPopupExtenderModifyQuizMoveTo.Show();
    }

    protected void Fill_ddlistModifyQuizMoveToSection(int CourseID)
    {
        ClassBLL classLogic = new ClassBLL();
        Classes.Courses_SectionDataTable sections = classLogic.GetSectionsByCourseID(CourseID, false);

        ddlistModifyQuizMoveToSection.Items.Clear();

        if (CourseID == 0)
        {
            ListItem firstItem = new ListItem("--- Select Class First ---", "0");
            ddlistModifyQuizMoveToSection.Items.Add(firstItem);
            ddlistModifyQuizMoveToSection.Enabled = false;
        }
        else
        {
            ListItem firstItem = new ListItem("--- Select Section if you'd like to ---", "0");
            ddlistModifyQuizMoveToSection.Items.Add(firstItem);

            foreach (Classes.Courses_SectionRow section in sections)
            {
                ListItem newItem = new ListItem(section.SectionNumber.ToString());
                ddlistModifyQuizMoveToSection.Items.Add(newItem);
            }

            ddlistModifyQuizMoveToSection.Enabled = true;
        }
    }

    protected void Fill_ddlistModifyEssayMoveToClass()
    {
        ClassBLL classLogic = new ClassBLL();
        Classes.CoursesDataTable courses = classLogic.GetCourses();

        ddlistModifyEssayMoveToClass.Items.Clear();

        ListItem firstItem = new ListItem("--- Select Class ---", "0");
        ddlistModifyEssayMoveToClass.Items.Add(firstItem);

        foreach (Classes.CoursesRow course in courses)
        {
            ListItem newItem = new ListItem(course.Title, course.CourseID.ToString());
            ddlistModifyEssayMoveToClass.Items.Add(newItem);
        }
        ddlistModifyEssayMoveToClass.SelectedIndex = 0;
    }

    protected void ddlistModifyEssayMoveToClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlistModifyEssayMoveToClass.SelectedIndex != 0)
        {
            int courseID = Convert.ToInt32(ddlistModifyEssayMoveToClass.SelectedValue);
            Fill_ddlistModifyEssayMoveToSection(courseID);
        }
        else
        {
            Fill_ddlistModifyEssayMoveToSection(0);
        }

        ddlistModifyEssayMoveToSection.SelectedIndex = 0;

        ModalPopupExtenderModifyEssayMoveTo.Show();
    }

    protected void Fill_ddlistModifyEssayMoveToSection(int CourseID)
    {
        ClassBLL classLogic = new ClassBLL();
        Classes.Courses_SectionDataTable sections = classLogic.GetSectionsByCourseID(CourseID, false);

        ddlistModifyEssayMoveToSection.Items.Clear();

        if (CourseID == 0)
        {
            ListItem firstItem = new ListItem("--- Select Class First ---", "0");
            ddlistModifyEssayMoveToSection.Items.Add(firstItem);
            ddlistModifyEssayMoveToSection.Enabled = false;
        }
        else
        {
            ListItem firstItem = new ListItem("--- Select Section if you'd like to ---", "0");
            ddlistModifyEssayMoveToSection.Items.Add(firstItem);

            foreach (Classes.Courses_SectionRow section in sections)
            {
                ListItem newItem = new ListItem(section.SectionNumber.ToString());
                ddlistModifyEssayMoveToSection.Items.Add(newItem);
            }

            ddlistModifyEssayMoveToSection.Enabled = true;
        }
    }

    protected void btnModifyQuizMoveTo_Click(object sender, EventArgs e)
    {
        int CourseID = Convert.ToInt32(ddlistModifyQuizMoveToClass.SelectedValue);
        int SectionNumber = Convert.ToInt32(ddlistModifyQuizMoveToSection.SelectedValue);
        bool result = true;

        if (CourseID == 0 || SectionNumber == 0)
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "Please select Course ID and Section Number.", 2);
            ModalPopupExtenderModifyQuizMoveTo.Show();
            return;
        }

        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected = XDocument.Parse(strModifySelected);

        List<XElement> _selected = xDocModifySelected.Descendants("selected").ToList();

        foreach (XElement selected in _selected)
        {
            int QuizSubmissionID = Convert.ToInt32(selected.Value);
            if (result == true)
            {
                result = submissionsLogic.MoveQuizSubmissionToOtherCourseSection(CourseID, SectionNumber, QuizSubmissionID);
            }
            else
            {
                ModalPopupMessage.ShowModalPopup("ILE Message", "Failed to move quiz submission(s).", 2);
                return;
            }
        }
        ModalPopupMessage.ShowModalPopup("ILE Message", "Quiz submission(s) has been moved successfully.", 1);
        Fill_RepeaterQuizSubmissions();
    }

    protected void btnModifyEssayMoveTo_Click(object sender, EventArgs e)
    {
        int CourseID = Convert.ToInt32(ddlistModifyEssayMoveToClass.SelectedValue);
        int SectionNumber = Convert.ToInt32(ddlistModifyEssayMoveToSection.SelectedValue);
        bool result = true;

        if (CourseID == 0 || SectionNumber == 0)
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "Please select Course ID and Section Number.", 2);
            ModalPopupExtenderModifyEssayMoveTo.Show();
            return;
        }

        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected = XDocument.Parse(strModifySelected);

        List<XElement> _selected = xDocModifySelected.Descendants("selected").ToList();

        foreach (XElement selected in _selected)
        {
            int EssaySubmissionID = Convert.ToInt32(selected.Value);
            if (result == true)
            {
                result = submissionsLogic.MoveEssaySubmissionToOtherCourseSection(CourseID, SectionNumber, EssaySubmissionID);
            }
            else
            {
                ModalPopupMessage.ShowModalPopup("ILE Message", "Failed to move essay submission(s).", 2);
                return;
            }
        }

        ModalPopupMessage.ShowModalPopup("ILE Message", "Essay submission(s) has been moved successfully.", 1);
        Fill_RepeaterEssaySubmissions();
    }

    protected void btnEssayChangeToSaved_Click(object sender, EventArgs e)
    {
        bool result = true;
        XDocument xDocModifySelected = new XDocument();
        xDocModifySelected = XDocument.Parse(strModifySelected);
        List<XElement> _selected = xDocModifySelected.Descendants("selected").ToList();

        foreach ( XElement selected in _selected )
        {
            int EssaySubmissionID = Convert.ToInt32(selected.Value);
            if (result)
            {
                result = submissionsLogic.UpdateEssaySubmission(submissionsLogic.GetEssaySubmission(EssaySubmissionID).StudentAnswer, false, EssaySubmissionID);
            }
            else
            {
                ModalPopupMessage.ShowModalPopup("ILE Message", "Failed to change the status of the selected essay submissions.", 2);
                return;
            }
        }

        ModalPopupMessage.ShowModalPopup("ILE Message", "The selected essay submissions' status has been changed to saved.", 1);
        Fill_RepeaterEssaySubmissions();
    }

#endregion

    #region [ Move Student ]

    protected void btnModifyStudentFind_Click(object sender, EventArgs e)
    {
        string NetworkID = txtModifyStudentNetworkID.Text;

        if (NetworkID == String.Empty || NetworkID == null)
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "Please put NetworkID", 2);
        }
        else
        {
            UsersBLL usersLogic = new UsersBLL();
            Users.UsersRow user = usersLogic.GetUserByUsername(NetworkID);

            if (user == null)
            {

            }
            else
            {
                Session["StudentUserID"] = user.UserID;
                ObjectDataSourceStudent.DataBind();
                DetailsViewStudent.DataBind();
            }

            Users._Relations_Course_UserDataTable relations = usersLogic.GetCourseUserRelationshipsByUserIdAndType(user.UserID, "Student");


        }


    }

    protected void rptrStudentClassList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }

    #endregion

    #endregion


}
